﻿using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using YbxLibrary;


namespace TestForm
{
    public partial class main : Form
    {
        public main()
        {
            InitializeComponent();
        }

        private static string formatJson(String str)
        {
            if (IsValidJson(str))
            {
                var jsonParsed = JToken.Parse(str);
                return jsonParsed.ToString(Formatting.Indented);
            }
            else
            {
                return str;
            }
        }
     
        private void button1_Click(object sender, EventArgs e)
        {
            textBox3.Text = "";

            string infno = textBox2.Text;
            string data = textBox1.Text;

            if (string.IsNullOrEmpty(infno)) {
                MessageBox.Show("交易编号不能为空");
                return;
            }

            if (string.IsNullOrEmpty(data))
            {
                MessageBox.Show("入参不能为空");
                return;
            }

            YbEntity ybEntity =  new YbEntity(textBox2.Text, data, textBox4.Text, textBox5.Text);
            Yb.YhHbCallLite(ref ybEntity);
            textBox1.Text = formatJson(ybEntity.input);
            if (ybEntity.infcode == 0)
            {
                textBox3.Text = formatJson(ybEntity.output);
            }
            else 
            {
                textBox3.Text = formatJson(ybEntity.output_json);
            }
        }
        
        private void Form1_Shown(object sender, EventArgs e)
        {
            this.textBox1.Text = @"{
                                     ""data"": {
                                         ""begntime"": """",
                                         ""card_sn"": """",
                                         ""certno"": """",
                                         ""mdtrt_cert_no"": ""420325199109067018"",
                                         ""mdtrt_cert_type"": ""02"",
                                         ""psn_cert_type"": """",
                                         ""psn_name"": """"
                                     }
                                 }";
        }

 
        public static bool IsValidJson(string jsonString)
        {
            try
            {
                JToken.Parse(jsonString);
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }


        private void textBox2_KeyUp(object sender, KeyEventArgs e)
        {
           
        }

        private void textBox2_KeyUp_1(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                textBox1.Text = formatJson(getInputData(textBox2.Text));
                textBox3.Text = "";
            }
        }

        private static string getInputData(string jybh) {
            string data = "";
            switch (jybh) {
                case "2601":  data = ParamIn.get2601();break;
                case "1101":  data = ParamIn.get1101(); break;
                default: data = ""; break;
            }
            return data;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            textBox1.Text = formatJson(textBox1.Text);
            string inputJson = Json.BuildJsonString(textBox2.Text, textBox1.Text, "", "", textBox4.Text, textBox5.Text, "");
            string outputJson = Yb.YhHbCall(textBox2.Text, inputJson);
            if (string.IsNullOrEmpty(outputJson)) {
                MessageBox.Show("返回值为空");
                return;
            }
            Dictionary<string, object> dict = JsonConvert.DeserializeObject<Dictionary<string, object>>(outputJson);
            if (dict["infcode"].ToString() == "0")
            {
                textBox3.Text = formatJson(dict["output"].ToString());
            }
            else
            {
                textBox3.Text = formatJson(outputJson);
            }
        }
    }
}
